package com.isoft.filelistener.flistener;

import com.isoft.filelistener.Utils.ApplicationContextProvider;
import com.isoft.filelistener.Utils.XmlUtils;
import com.isoft.filelistener.entity.*;
import com.isoft.filelistener.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Component
@Slf4j
public class FileListeners implements FileAlterationListener {
    private String driction=null;
    @Override
    public void onDirectoryChange(File file) {
        log.info("文件夹修改.....");
    }

    @Override
    public void onDirectoryCreate(File file) {
        log.info(file.getName()+"创建文件夹");
        driction=file.getName();
    }

    @Override
    public void onDirectoryDelete(File file) {
        log.info("文件夹删除.....");
    }

    @Override
    public void onFileChange(File file) {
        log.info("文件修改.....");
    }

    @Override
    public void onFileCreate(File file) {
        String name=file.getName();
        switch (name.substring(0,9)){
            case "DFME-DEPE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    DepeMsg depeMsg = XmlUtils.parseXml(inputStream, DepeMsg.class); //将xml文件转化为java对象(Msg类的实例)
                    log.info(depeMsg + "");
                    ApplicationContextProvider.getBean(DepeMsgService.class).insertdepe(depeMsg.getMeta(), depeMsg.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-DLYE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    DlyeMsg dlyeMsg = XmlUtils.parseXml(inputStream, DlyeMsg.class); //将xml文件转化为java对象(Msg类的实例)
                    log.info(dlyeMsg + "");
                    ApplicationContextProvider.getBean(DlyeMsgService.class).insertdlye(dlyeMsg.getMeta(), dlyeMsg.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-FETT":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    FettMsg fettMsg = XmlUtils.parseXml(inputStream, FettMsg.class); //将xml文件转化为java对象(Msg类的实例)
                    log.info(fettMsg + "");
                    ApplicationContextProvider.getBean(FettMsgService.class).insertfett(fettMsg.getMeta(), fettMsg.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-FPTT":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    FpttMsg fpttMsg = XmlUtils.parseXml(inputStream, FpttMsg.class); //将xml文件转化为java对象(Msg类的实例)
                    log.info(fpttMsg + "");
                    ApplicationContextProvider.getBean(FpttMsgService.class).insertfptt(fpttMsg.getMeta(), fpttMsg.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-FRTT":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    FrttMsg frttMsg = XmlUtils.parseXml(inputStream, FrttMsg.class); //将xml文件转化为java对象(Msg类的实例)
                    log.info(frttMsg + "");
                    ApplicationContextProvider.getBean(FrttMsgService.class).insertfrtt(frttMsg.getMeta(), frttMsg.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-GTLS":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream + "");
                    GtlsMsg gtlsMsg = XmlUtils.parseXml(inputStream, GtlsMsg.class); //将xml文件转化为java对象(Msg类的实例)
                    log.info(gtlsMsg + "");
                    ApplicationContextProvider.getBean(GtlsMsgService.class).insertgtls(gtlsMsg.getMeta(), gtlsMsg.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            default:
                log.info("没有此文件");
        }
    }

    @Override
    public void onFileDelete(File file) {

        log.info("文件删除.....");
    }

    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {
        log.info("监听开始3.....");
    }

    @Override
    public void onStop(FileAlterationObserver fileAlterationObserver) {
        log.info("监听结束3.....");
    }
}
